package com.ocom.accesservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.accesservice.dao.IDaoMjRegionInfo;
import com.ocom.accesservice.service.MjRegionInfoService;
import com.ocom.common.entity.access.EntityMjRegionInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MjRegionInfoServiceImpl extends ServiceImpl<IDaoMjRegionInfo, EntityMjRegionInfo> implements MjRegionInfoService {

    @Autowired
    private IDaoMjRegionInfo iDaoMjRegionInfo;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjRegionInfo> selectMjRegionInfoList(Long comId, JSONObject object) {
        QueryWrapper<EntityMjRegionInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("com_id",comId);
        if (object.getLong("regionId")!=null && object.getLong("regionId")!=-1l && object.getLong("regionId")!=0l){
            wrapper.eq("region_id",object.getLong("regionId"));
        }
        if (object.getLong("deptId")!=null && object.getLong("deptId")!=-1){
            wrapper.eq("dept_id",object.getLong("deptId"));
        }
        if (StringUtils.isNotEmpty(object.getString("regionName"))){
            wrapper.like("region_name",object.getString("regionName"));
        }
        return iDaoMjRegionInfo.selectList(wrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public PageInfo<EntityMjRegionInfo> selectPageInfoList(Long comId, JSONObject object) {
        QueryWrapper<EntityMjRegionInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("com_id",comId);
        if (object.getLong("regionId")!=null && object.getLong("regionId")!=-1 && object.getLong("regionId")!=0){
            wrapper.eq("region_id",object.getLong("regionId"));
        }
        if (object.getLong("deptId")!=null && object.getLong("deptId")!=-1){
            wrapper.eq("dept_id",object.getLong("deptId"));
        }
        if (StringUtils.isNotEmpty(object.getString("regionName"))){
            wrapper.like("region_name",object.getString("regionName"));
        }
        PageHelper.startPage(object.getInteger("page"),object.getInteger("size"));
        List<EntityMjRegionInfo> pointInfoPageInfo=this.list(wrapper);
        PageInfo<EntityMjRegionInfo> pageInfo=new PageInfo<>(pointInfoPageInfo);
        return pageInfo;
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjRegionInfo selectRegionName(Long comId, String regionName) {
        QueryWrapper<EntityMjRegionInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("region_name",regionName);
        wrapper.eq("com_id",comId);
        wrapper.last("limit 1");
        return iDaoMjRegionInfo.selectOne(wrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public int insertEntityMjRegionInfo(Long comId, EntityMjRegionInfo saveEntityMjRegionInfo) {
        return iDaoMjRegionInfo.insert(saveEntityMjRegionInfo);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public int updateEntityMjRegionInfo(Long comId, EntityMjRegionInfo saveEntityMjRegionInfo) {
        return iDaoMjRegionInfo.updateById(saveEntityMjRegionInfo);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public int removeRegionInfo(Long comId, Long regionId) {
        QueryWrapper<EntityMjRegionInfo> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("region_id",regionId);
        return iDaoMjRegionInfo.delete(query);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId",isMaster = MasterSlaveEnum.MASTER)
    public EntityMjRegionInfo selectMjRegionInfo(Long companyId, EntityMjRegionInfo entityMjRegionInfo) {
        return this.getOne(Wrappers.lambdaQuery(EntityMjRegionInfo.class)
                .eq(EntityMjRegionInfo::getRegionId,entityMjRegionInfo.getRegionId())
                .eq(EntityMjRegionInfo::getComId,entityMjRegionInfo.getComId())
        );
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId",isMaster = MasterSlaveEnum.MASTER)
    public List<EntityMjRegionInfo> selectMjRegionByIds(Long companyId, List<Long> ids) {
        return this.list(Wrappers.lambdaQuery(EntityMjRegionInfo.class).eq(EntityMjRegionInfo::getComId,companyId).in(EntityMjRegionInfo::getRegionId,ids));
    }
}
